<?php
namespace common\core\base;

use yii\web\Application;

/**
 * BaseApplication class, contains loadActiveModules function
 * 
 * @author Xuegui Yuan <yuanxuegui@163.com>
 */
class BaseApplication extends Application
{

    public $activeModules = [];

    public function loadActiveModules($app)
    {
        $moduleManager = getModularityService();
        
        $this->activeModules = $moduleManager->getActiveModules($app);
        
        foreach ($this->activeModules as $m) {
            $moduleId = $m['id'];
            $moduleDir = $m['dir'];
            $ModuleClassName = $m['dir_class'];
            
            $this->setModule($moduleId, [
                'class' => 'common\modules\\' . $moduleDir . "\\$app\\Module"
            ]);
            
            $serviceFile = \Yii::getAlias('@common') . '\modules\\' . $moduleDir . '\\' . $ModuleClassName . 'Service.php';
            if (file_exists($serviceFile)) {
                $serviceClass = 'common\modules\\' . $moduleDir . '\\' . $ModuleClassName . 'Service';
                $serviceInstance = new $serviceClass();
                $this->set($serviceInstance->getServiceId(), $serviceInstance);
            }
        }
    }
}